package com.app.megaprosoft.modyimcarcare;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.StrictMode;
import android.util.Log;

@SuppressLint("NewApi")
public class connect {
	String que;
	String u,pa,da,ser;
	Connection connect;
	
	public connect(){
		u = "sa";
		pa = "mega1234";
		da = "MegaAutoCare";
		ser = "mega-autocare.dyndns.biz:1433";	
		//ser = "192.168.10.22:1433";	
		connect = CONN(u, pa, da, ser);
	}
	
	
	public connect(String user, String pass, String database, String server){
		u = user;
		pa = pass;
		da = database;
		ser = server;	
		connect = CONN(u, pa, da, ser);
	}
	

	
	
	
	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	@SuppressLint("NewApi")
	public Connection CONN(String _user, String _pass, String _DB, String _server )
	{
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);
		Connection conn = null;
		String ConnURL = null;
		try {
		
			Class.forName("net.sourceforge.jtds.jdbc.Driver");
			ConnURL = "jdbc:jtds:sqlserver://" + _server + ";" + "databaseName=" + _DB + ";user=" + _user + ";password=" + _pass + ";";
			conn = DriverManager.getConnection(ConnURL);		
		} catch (SQLException se) {
			Log.e("ERRO",se.getMessage());
		} catch (ClassNotFoundException e) {
			Log.e("ERRO",e.getMessage());
		} catch (Exception e) {
		    Log.e("ERRO",e.getMessage());
		}
		
		return conn;
	}
	
	
	
	public ResultSet selectData(String COMANDOSQL){
		ResultSet rs = null;
		try {
		   
			Statement statement = connect.createStatement();
			rs = statement.executeQuery(COMANDOSQL);
			// Configurando nosso SimpleAdapter
		} catch (Exception e) {
        Log.e("ERRO",e.getMessage());
		}
		return rs;
		
	}   
	
	
	public boolean addData(String sql){
		Statement statement = null;
		try {   
			 statement = connect.createStatement();
			 statement.execute(sql);
			 return true;
	
			 
		} catch (Exception e) {
			 return false;
		}

	}
	
	

	public boolean deleteData(String sql){
		try {   
			
			Statement statement = connect.createStatement();
			statement.execute(sql);
			 return true;
			 
		} catch (Exception e) {
			 return false;
		}
	}
	

	public boolean editData(String sql){
		try {   
			
			Statement statement = connect.createStatement();
			statement.execute(sql);
			 return true;
			 
		} catch (Exception e) {
			 return false;
		}
	}
	
	



}
